/**********************************************************************************
* CLASS
*		ImageViewBar
*
* DESCRIPTION
*		Generate Image view bar object for display list of the image. Have 2 Mode
*			1. View Menu
*			2. View Image
*
* REVISION HISTORY
*		Revision 1.0 2008/10/12 Atthaboon S.
*		First Release
*
**********************************************************************************/
var ImageViewBar = Class.create({

	/****************************************************
	* FUNCTION
	*		initialize
	*
	* DESCRIPTION
	* 		initialize the Property Image View Bar.
	*
	* ARGUMENTS
	*		array_img		:	array of image src file
	*		imagewidth		:	image width
	*		imageheight	:	image height
	*		row			: 	image row
	*		column		:	image column
	*
	****************************************************/
	initialize: function(array_img,imagewidth,imageheight,row,column)
	{
		this.currentPage = 1;
	
		this.imageArray = new Array();
		this.imageDomArray = new Array();
		
		this.imagewidth  = imagewidth;
		this.imageheight = imageheight;
	
		this.row    = row;
		this.column = column;
	
		// Backup all image in imageArray
		this.imageArray = array_img;
		this.table      = new Element('table',{'style':'width:100%;height:100%;'});
		
		var tableBody  = new Element('tbody',{});
		var img_count  = 0;
		for(var i =0;i<row;i++)
		{
			var tr = new Element('tr',{})
			for(var j =0;j<column;j++)
			{
				var td = new Element('td',{'width':(100/column)+'%','height':(100/row)+'%','align':'center','valign':'middle'});
				if(img_count < array_img.length)
				{
					var img = new BookImage(array_img[img_count],0,0);
					img.setSize(this.imagewidth,this.imageheight);
					img.getDomObj().style.position = ''; // remove position absolute.
					img.getDomObj().style.left     = '';
					img.getDomObj().style.top      = '';
					td.appendChild(img.getDomObj());

					// Image object from          Page,image number
					this.imageDomArray[img_count] = img;
					
					img_count++;
				}
				tr.appendChild(td);
			}
			tableBody.appendChild(tr);
		}
		this.table.appendChild(tableBody);
	},
	
	/****************************************************
	* FUNCTION 
	*		getDomType
	*
	* DESCRIPTION
	* 		Use by Genesis for get Genesis dom Component Type.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomType : function()
	{
		return "ImageViewBar";
	},		

    /****************************************************
	* FUNCTION 
	*		getDomObj
	*
	* DESCRIPTION
	* 		Use by Genesis for get Image View Bar dom Component.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomObj : function()
	{
		return this.table;
	},
	
	/****************************************************
	* FUNCTION
	*		getCurrentPage
	*
	* DESCRIPTION
	* 		Get the current image page.
	*
	* ARGUMENTS
	*
	****************************************************/
	getCurrentPage : function()
	{
		return this.currentPage;
	},
	
	/****************************************************
	* FUNCTION
	*		getNextPage
	*
	* DESCRIPTION
	* 		View next page..
	*
	* ARGUMENTS
	*
	****************************************************/
	getNextPage: function()
	{
		var startPoint = (this.currentPage) * (this.row * this.column);
		if(startPoint > this.imageArray.length)
		{
			// No image for display
		}
		else
		{
			this.currentPage = this.currentPage+1;
			this.getPage(this.currentPage);
		}
	},
	
	/****************************************************
	* FUNCTION
	*		getPreviousPage
	*
	* DESCRIPTION
	* 		View previous page..
	*
	* ARGUMENTS
	*
	****************************************************/
	getPreviousPage: function()
	{
		if(this.currentPage > 1)
		{
			this.currentPage = this.currentPage-1;
			this.getPage(this.currentPage);
		}
	},	
	
	/****************************************************
	* FUNCTION
	*		getSpecificPage
	*
	* DESCRIPTION
	* 		Get the specific page.
	*
	* ARGUMENTS
	*		page	:	page view from (0,.....n)		
	*
	****************************************************/
	getSpecificPage: function(page)
	{
		var startPoint = (page-1) * (this.row * this.column);
		if(page > 0 && !(startPoint > this.imageArray.length))
		{
			this.currentPage = page;
			this.getPage(this.currentPage);
		}
	},
	
	/****************************************************
	* FUNCTION
	*		getPage
	*
	* DESCRIPTION
	* 		View specific page..
	*
	* ARGUMENTS
	*		page	:	page view from (0,.....n)		
	*
	****************************************************/
	getPage: function(page)
	{
		var startPoint = (parseInt(''+page)-1) * (this.row * this.column);
		if(startPoint > this.imageArray.length)
		{
			// No image
			return;
		}
		
		//Remove All Childe node
		for(var i = 0; i < this.table.childNodes.length; i++)
	    {
			this.table.removeChild(this.table.childNodes[i]);
	    }
		
		var tableBody  = new Element('tbody',{});
		var img_count  = startPoint;
		
		for(var i =0;i<this.row;i++)
		{
			var tr = new Element('tr',{})
			for(var j =0;j<this.column;j++)
			{
				var td = new Element('td',{'width':(100/column)+'%','height':(100/row)+'%','align':'center','valign':'middle'});
				if(img_count < this.imageArray.length)
				{
					if(this.imageDomArray[img_count] == "undefined" || this.imageDomArray[img_count] == null)
					{
						var img = new BookImage(this.imageArray[img_count],0,0);
						img.setSize(this.imagewidth,this.imageheight);
						img.getDomObj().style.position = ''; // remove position absolute.
						img.getDomObj().style.left     = '';
						img.getDomObj().style.top      = '';
						td.appendChild(img.getDomObj());

						// Image object from          Page,image number
						this.imageDomArray[img_count] = img;
					}
					else
					{
						td.appendChild(this.imageDomArray[img_count].getDomObj());
					}
					img_count++;
				}
				tr.appendChild(td);
			}
			tableBody.appendChild(tr);
		}
		this.table.appendChild(tableBody);
	}
});